package com.c.ctgproject.ui.collect

import androidx.recyclerview.widget.LinearLayoutManager
import com.c.ctgproject.adapter.guang.PartHandlerAdapter
import com.c.ctgproject.base.BaseActivityV2
import com.c.ctgproject.databinding.ActivityPartHandlerOverBinding
import com.c.ctgproject.entity.ResultEntity
import com.c.ctgproject.entity.guang.PartHandlerListEntity
import com.c.ctgproject.entity.guang.PartOutListEntity
import com.c.ctgproject.event.PositionEvent
import com.c.ctgproject.http.ApiMethods
import com.c.ctgproject.http.MyObserverHandlerLoading
import com.c.ctgproject.utils.*
import org.greenrobot.eventbus.Subscribe
import org.jetbrains.anko.startActivity

/**
 *作者:LiBW
 *创建日期: 2021/9/17
 *描述:零件拆解单 页面
 */
class PartHandlerOverActivity : BaseActivityV2(true), PartHandlerAdapter.PartHandlerAdapterListener {
    private val mBinding by lazy { ActivityPartHandlerOverBinding.inflate(layoutInflater) }
    override fun getLayout() = mBinding.root
    private var search = ""
    private var page = 1
    private val mList = ArrayList<PartHandlerListEntity.DataBean.ListBean>()
    private val mAdapter by lazy { PartHandlerAdapter(this, mList, this) }

    @Subscribe
    fun event(type: PositionEvent) {
        if (type.position != -1) {
            mList.removeAt(type.position)
            mAdapter.notifyItemRemoved(type.position)
        }
    }

    override fun event() {
        getData()
        mBinding.mRecyclerView.adapter = mAdapter
        mBinding.mRecyclerView.layoutManager = LinearLayoutManager(this)
    }


    private var statue = 1
    private var searchMine = false
    override fun click() {
//        状态 1-拆解中 2-拆解完成
        mBinding.mTabLayout.handlerSelectReturnText {
            when (it) {
                "拆解中" -> {
                    searchMine = false
                    statue = 1
                    page = 1
                    getData()
                }
                "我的" -> {
                    searchMine = true
                    statue = 2
                    page = 1
                    getData()
                }
            }
        }
        mBinding.mSearchET.handlerText {
            if (it.isEmpty() || it == "") {
                search = ""
                page = 1
                getData()
            }
        }
        mBinding.mSearchTV.setOnClickListener {
            search = mBinding.mSearchET.text.handlerNull()
            page = 1
            getData()
        }
        mBinding.mRefresh.setOnLoadMoreListener {
            page++
            getData()
        }

        mBinding.mRefresh.setOnRefreshListener {
            page = 1
            getData()
        }
    }

    private fun getData() {
        if (searchMine) {
            //状态 1-拆解中 2-拆解完成 (根据状态查询 新建是1 我的拆解单列表是2)
            ApiMethods.apiSubscribe(ApiMethods.retrofit.myPartHandlerOrder(search, page, 2), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
                MyLog.d("我的零部件拆解单回调:$it")
                val data = getGson().fromJson(it, PartHandlerListEntity::class.java).data
                data?.list?.let { list ->
                    if (page == 1) mList.clear()
                    mList.addAll(list)
                    mAdapter.notifyDataSetChanged()
                    mBinding.mRecyclerView.handlerPosition(page)

                }
            })
        } else {
            ApiMethods.apiSubscribe(ApiMethods.retrofit.partHandlerList(search, page, statue), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
                MyLog.d("零部件拆解单回调:$it")
                val data = getGson().fromJson(it, PartHandlerListEntity::class.java).data
                data?.list?.let { list ->
                    if (page == 1) mList.clear()
                    mList.addAll(list)
                    mAdapter.notifyDataSetChanged()
                    mBinding.mRecyclerView.handlerPosition(page)
                }
            })
        }
    }


    override fun onHandlerPart(position: Int) {
        startActivity<PartDestroyActivity>(
            MyConstants.POSITION to position,
            MyConstants.ID to mList[position].orderId
        )


    }

    override fun itemDelete(position: Int, data: PartHandlerListEntity.DataBean.ListBean) {
//        ApiMethods.apiSubscribe(ApiMethods.retrofit.deleteParts(data?.partsId:0), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
//            MyLog.d("删除结果:$it")
//            val result= getGson().fromString<ResultEntity<String>>(it)
//            if(result.status==1){
//                mList.removeAt(position)
//                mAdapter.notifyDataSetChanged()
//            }
//        })
    }
}